if (!window.SinoPortal)
	window.SinoPortal = {};

SinoPortal.noOpera = function(){
	return;
};

SinoPortal.Login = {
	initLogin : function(){
		var w = $("[id='login:pageWidth']");
		var h = $("[id='login:pageHeight']");
		
		var ws = SinoPortal.Utils.getWindowSize();
		
		w.val(ws.width);
		h.val(ws.height);
	}
};

SinoPortal.View = {
	
	/**
	 * 通过命令创建Tab
	 * 
	 * @param sceneId 		场景ID
	 * @param layoutId 		布局ID
	 * @param parentId 		父节点ID
	 * @param params 		参数
	 * 		displayName 	显示名称
	 * 		image16 		显示图片
	 * 		closable 		是否可以关闭
	 * 		其他自定义参数
	 */
	createTab : function(sceneId,layoutId,parentId,params){
		statusDialog.show();
		PrimeFaces.ab({
			formId : 'portalForm',
			source : 'portalForm:portalCommand',
			process : '@all',
			oncomplete : function(xhr, status, args){
				SinoPortal.NaviMenu.onClickMenuItemComplete(xhr, status, args);
			},
			params : {
				actionType : 'tab',
				sceneId : sceneId,
				layoutId : layoutId,
				parentId : parentId,
				params : SinoPortal.Utils.toJSON(params)
			}
		});
	},
	
	/**
	 * 通过ViewBean ID改变tab
	 * 
	 * @param viewBeanId 		ViewBean ID
	 */
	changeTab : function(viewBeanId){
		var ts = jQuery("#main-tabSet");
		var divs = ts.children("div");
		var tabId = viewBeanId + "_tab";
		var tabIdx = -1;
		for(var i=0;i<divs.length;i++){
			if(tabId == divs[i].id){
				tabIdx = i;
				break;
			}
		}
		
		if(tabIdx != -1){
			main_tabSet_var.select(tabIdx);
		}
	},
	
	/**
	 * 通过ViewBean ID改变tab
	 * 
	 * @param viewBeanId 		ViewBean ID
	 * @param flag				是否改变
	 */
	modifyTabFlag : function(viewBeanId, flag){
		var ts = jQuery("#main-tabSet");
		var divs = ts.children("div");
		var tabId = viewBeanId + "_tab";
		var tabIdx = -1;
		for(var i=0;i<divs.length;i++){
			if(tabId == divs[i].id){
				tabIdx = i;
				break;
			}
		}
		
		if(tabIdx != -1){
			main_tabSet_var.modifyTabFlag(tabIdx, flag);
		}
	},
	
	/**
	 * 通过命令创建Win
	 * 
	 * @param sceneId 		场景ID
	 * @param layoutId 		布局ID
	 * @param parentId 		父节点ID
	 * @param params 		参数
	 * 		displayName 	显示名称
	 * 		image16 		显示图片
	 * 		width 			宽度
	 * 		height			高度
	 * 		minWidth		最小宽度
	 * 		minHeight		最小高度
	 * 		resizeable		是否可以调整尺寸
	 * 		draggable		是否可以移动
	 * 		modal			是否显示蒙版
	 * 		其他自定义参数
	 */
	createWin : function(sceneId,layoutId,parentId,params){
		statusDialog.show();
		PrimeFaces.ab({
			formId : 'portalForm',
			source : 'portalForm:portalCommand',
			process : '@all',
			oncomplete : function(xhr, status, args){
				SinoPortal.NaviMenu.onClickMenuItemComplete(xhr, status, args);
			},
			params : {
				actionType : 'window',
				sceneId : sceneId,
				layoutId : layoutId,
				parentId : parentId,
				params : SinoPortal.Utils.toJSON(params)
			}
		});
	},
	
	/**
	 * 窗口显示事件
	 */
	winOnShow : function(winId,contentViewBeanId,show){
		SinoPortal.View.initEditor(winId);
		if(show) {
			show.call(this, winId, contentViewBeanId);
	    }
	},
	
	/**
	 * 窗口关闭事件
	 */
	winOnHide : function(winId,contentViewBeanId,hide){
		SinoPortal.View.removeEditor(winId);
		if(hide) {
			hide.call(this, winId, contentViewBeanId);
	    }
		portal_window_close(winId);
	},
	
	/**
	 * Tab显示事件
	 */
	onTabShow : function(index){
		var panel = main_tabSet_var.get(index);
		SinoPortal.View.initEditorEl(jQuery(panel));
		SinoPortal.View.initMainView();
	},
	
	/**
	 * Tab关闭事件
	 */
	onTabClose : function(index){
		var panel = main_tabSet_var.get(index);
		SinoPortal.View.removeEditorEl(jQuery(panel));
	},
	
	EDITOR_CATCH : {},
	
	initEditor : function(parentId){
		var el = jQuery(PrimeFaces.escapeClientId(parentId));
		SinoPortal.View.initEditorEl(el);
	},
	
	initEditorEl : function(el){
		var editor = el.find("textarea.portal-editor");
		if(editor && editor.length > 0){
			for(var i=0;i<editor.length;i++){
				var id = editor.get(i).id;
				if(!SinoPortal.View.EDITOR_CATCH[id]){
					var ck = CKEDITOR.replace(id);
					eval("jQuery.extend(SinoPortal.View.EDITOR_CATCH,{'"+id+"':ck});");
				}
			}
		}
	},
	
	removeEditor : function(parentId){
		var el = jQuery(PrimeFaces.escapeClientId(parentId));
		SinoPortal.View.removeEditorEl(el);
	},
	
	removeEditorEl : function(el){
		var editor = el.find("textarea.portal-editor");
		if(editor && editor.length > 0){
			for(var i=0;i<editor.length;i++){
				var id = editor.get(i).id;
				SinoPortal.View.EDITOR_CATCH[id]=null;
			}
		}
	},
	
	updateEditor : function(parentId){
		var editor = jQuery(PrimeFaces.escapeClientId(parentId)).find("textarea.portal-editor");
		if(editor && editor.length > 0){
			for(var i=0;i<editor.length;i++){
				var id = editor.get(i).id;
				var ck = SinoPortal.View.EDITOR_CATCH[id];
				jQuery(PrimeFaces.escapeClientId(id)).html(ck.getData());
			}
		}
	},
	
	/**
	 * 初始化视图区尺寸
	 */
	initObjectView : function(){
		var ovs = jQuery(".portal-objectView");
		var contHeight = jQuery(jQuery("#portalObject .ui-layout-unit-content").get(0)).height();
		ovs.each(function(i){
			var ov = jQuery(this);
			var ovtabs = ov.find(".ui-accordion-content");
			if(ovtabs){
				var ovtabHeight = contHeight - 31*ovtabs.length -22;
				ovtabs.each(function(j){
					jQuery(this).height(ovtabHeight);
				});
			}
			if(i>0){
				ov.hide();
			}
		});
	},
	
	/**
	 * 初始化主视图尺寸
	 */
	initMainView : function(){
		var pm = jQuery("#main-tabSet");
		var pmul = jQuery("#main-tabSet ul");
		var pmcon = jQuery("#main-tabSet .portal-mainView-tab-content");
		pmcon.outerHeight(pm.height()-pmul[0].clientHeight);
		
		// home area
		var contWidth = pm.width();
		var home = jQuery(".portal-mainView-home");
		var colNum = home.children().length;
		var colWidth = ( contWidth - 24 * (colNum+1) ) / colNum;
		var cols = home.find(".ui-dashboard-column");
		cols.width(colWidth);
		cols.css("padding-right","20px");
		jQuery(cols.get(colNum-1)).css("padding-right","0px");
		
		// iframe
		var contentFrame = jQuery("#main-tabSet .portal-mainView-tab-content-iframe");
		jQuery.each(contentFrame, function(i){
			var fr = jQuery(this);
			var pfr = fr.parent();
			if(pfr.width()>0){
				var w = pfr.width(),
				h = pfr.height();
				fr.height(h);
			}
		});
		
		// layout area
		var contentLayout = jQuery("div.portal-mainView-tab-content-layout");
		contentLayout.outerHeight(pmcon.height());
		
		var layouts = jQuery("div.sino-layout-container");
		jQuery.each(layouts, function(i){
			var lo = jQuery(this);
			var plo = lo.parent();
			if(lo.parent().width()>0){
				var low = lo.parent().width();
				var loh = lo.parent().height();
				var lo_north = jQuery(lo.children(".ui-layout-pane-north"));
				var lo_west = jQuery(lo.children(".ui-layout-pane-west"));
				var lo_center = jQuery(lo.children(".ui-layout-pane-center"));
				var lo_east = jQuery(lo.children(".ui-layout-pane-east"));
				var lo_south = jQuery(lo.children(".ui-layout-pane-south"));
				var lo_s_w = lo_s_h = 6;
				var lo_padding_w = lo_padding_h = 5;
				var lo_border_w = lo_border_h = 1;
		
				// exist
				var north_e=false,south_e=false,west_e=false,east_e=false,center_e=false;
				// location
				var left=0,top=0,bottom=0,right=0;
				// north
				lo_north.width(low - 2*(lo_padding_w+lo_border_w));
				if(lo_north.length>0){
					north_e = true;
				}
				var lo_north_h = lo_north.height();
				if(lo_north_h<loh*0.35){
					lo_north.height(lo_north_h);	
				}else{
					lo_north.height(loh*0.35);
				}
				lo_north.css({
					"margin" : 0,
					"padding" : lo_padding_w+"px",
					"position" : "absolute",
					"left" : left+"px",
					"right" : "auto",
					"top" : top+"px",
					"bottom" : "auto"
				});
				
				//south
				var lo_south_h = lo_south.height();
				if(lo_south_h<loh*0.35){
					lo_south.height(lo_south_h);	
				}else{
					lo_south.height(loh*0.35);
				}
				lo_south.width(low - 2*(lo_padding_w+lo_border_w));
				if(lo_south.length>0){
					south_e = true;
				}
				
				var lo_south_h = lo_south.height();
				lo_south.css({
					"margin" : 0,
					"padding" : lo_padding_w+"px",
					"position" : "absolute",
					"left" : left+"px",
					"right" : "auto",
					"top" : "auto",
					"bottom" : bottom+"px"
				});
				
				// west
				if(lo_west.length>0){
					west_e = true;
				}
				
				var lo_west_w = lo_west.width();
				lo_west.css({
					"margin" : 0,
					"padding" : lo_padding_w+"px",
					"position" : "absolute",
					"left" : left+"px",
					"right" : "auto",
					"top" : top+"px",
					"bottom" : bottom+"px"
				});
				
				// east
				if(lo_east.length>0){
					east_e = true;
				}
				// TODO lo_border_w
				var lo_east_w = lo_east.width();
				lo_east.css({
					"margin" : 0,
					"padding" : lo_padding_w+"px",
					"position" : "absolute",
					"left" : "auto",
					"right" : right+"px",
					"top" : top+"px",
					"bottom" : bottom+"px"
				});
				
				// center
				// TODO lo_border_w
				if(east_e){
					var lo_center_w = low - lo_west.outerWidth() - lo_s_w - 3*(lo_padding_w+lo_border_w) - lo_east.outerWidth();
				}else if(north_e){
				    var lo_center_w = low - 2*(lo_padding_w+lo_border_w);
				}else{
					var lo_center_w = low - lo_west.outerWidth() - lo_s_w - 2*(lo_padding_w+lo_border_w);
				}
				
				if(north_e){
				    var lo_center_h = loh - lo_north.outerHeight() - lo_s_h - 3*(lo_padding_h+lo_border_h) - lo_south.outerHeight();
				}else{
					var lo_center_h = loh - lo_s_h - (lo_padding_h+lo_border_h);
				}
				
				if(lo_west.outerWidth()!=null){
				    var left = lo_west.outerWidth() + lo_s_w;
				}
				
				if(north_e){
				    var top = lo_north.outerHeight() + lo_s_h;
				}
				
				lo_center.width(lo_center_w);
				lo_center.height(lo_center_h);
				lo_center.css({
					"margin" : 0,
					"padding" : lo_padding_w+"px",
					"position" : "absolute",
					"left" : left+"px",
					"right" : "auto",
					"top" : top+"px",
					"bottom" : bottom+"px"
				});
			}
		});
	}
};

SinoPortal.PageView = {
	dataView : {
		click : function(id,t){
			var ul = jQuery(PrimeFaces.escapeClientId(id));
			ul.children("li").removeClass("portal-dataview-li-selected");
			jQuery(t).addClass("portal-dataview-li-selected");
		},
		getSelected : function(id){
			var ul = jQuery(PrimeFaces.escapeClientId(id));
			return ul.children("li.portal-dataview-li-selected");
		}
	}
};

SinoPortal.Utils = {
	/**
	 * 判断对象为空
	 * 
	 */
	isEmpty : function(obj){
		if(obj && obj != null && obj != "" 
			&& obj != "undefined" && obj!="null"){
			return false;
		}
		return true;
	},
    /**
     * @see  将json字符串转换为对象
     * @param   json字符串
     * @return 返回object,array,string等对象
     */
	evalJSON:function (strJson){
		return eval("("+strJson+")");			
	},
	
	/**
	 * @see  将javascript数据类型转换为json字符串
	 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp
	 * @return 返回json字符串
	 */
	toJSON:function (object){
		if(!object){
			return "{}";
		}
		var type=typeof object;			
		if('object'==type){
			if(Array==object.constructor )
			type='array';				
			else if(RegExp==object.constructor )
			type='regexp';				
			else 
			type='object';
		}
		switch(type){
			case 'undefined':
			case 'unknown':
			return ;				
			break;				
			case 'function':
			case 'boolean':
			case 'regexp':
			return object.toString ();				
			break;				
			case 'number':
			return isFinite(object)?object.toString ():'null';				
			break;				
			case 'string':
			return '"'+object.replace(/(\\|\")/g,"\\$1").replace(/\n|\r|\t/g,
			function (){
				var a=arguments[0];
				return (a=='\n')?'\\n':
				(a=='\r')?'\\r':
				(a=='\t')?'\\t':""
			})+'"';				
			break;				
			case 'object':
			if(object===null)return 'null';				
			var results=[];				
			for(var property in object){
				var value=SinoPortal.Utils.toJSON(object[property]);					
				if(value!==undefined)
				results.push(SinoPortal.Utils.toJSON(property)+':'+value);
			}
			return '{'+results.join(',')+'}';				
			break;				
			case 'array':
			var results=[];				
			for(var i=0;i<object.length;i++){
				var value=SinoPortal.Utils.toJSON(object[i]);					
				if(value!==undefined)results.push(value);
			}
			return '['+results.join(',')+']';				
			break;
		}
	},
	
	getWindowSize : function() {
	    var myWidth = 0, myHeight = 0;
	    if( typeof( window.innerWidth ) == 'number' ) {
	        myWidth = window.innerWidth;
	        myHeight = window.innerHeight;
	    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	        myWidth = document.documentElement.clientWidth;
	        myHeight = document.documentElement.clientHeight;
	    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	        myWidth = document.body.clientWidth;
	        myHeight = document.body.clientHeight;
	    }
		return {"width":myWidth,"height":myHeight};
	}
	
};

SinoPortal.NaviMenu = {
	
	config : {
		leftMenuNum : 0,
		maxShowMenuNum : 7
	},
	
	changeLeftRightClass : function(n){
		// 向左的按钮
		if(SinoPortal.NaviMenu.config.leftMenuNum>0){
			jQuery("#portalMainMenuLeft").removeClass('ui-state-disabled');
		}else{
			jQuery("#portalMainMenuLeft").addClass('ui-state-disabled');
		}
		
		// 向右的按钮
		if( (n-SinoPortal.NaviMenu.config.maxShowMenuNum) > SinoPortal.NaviMenu.config.leftMenuNum){
			jQuery("#portalMainMenuRight").removeClass('ui-state-disabled');
		}else{
			jQuery("#portalMainMenuRight").addClass('ui-state-disabled');
		}
	},
	
	/**
	 * 单击菜单的向左按钮
	 * @param t 点击事件的对象
	 */
	onClickLeft : function(t,n){
		if(SinoPortal.NaviMenu.config.leftMenuNum>0){
			SinoPortal.NaviMenu.config.leftMenuNum -= 1;
			var left = SinoPortal.NaviMenu.config.leftMenuNum * 80 * -1;
			jQuery("#portalMainMenuBody").animate({"left":left},"slow");
			
			// 修改左右的状态
			SinoPortal.NaviMenu.changeLeftRightClass(n);
		}
	},
	
	onMouseOverLeft : function(t,n){
		if(SinoPortal.NaviMenu.config.leftMenuNum>0){
			jQuery(t).addClass('ui-state-hover');
		}
	},
	
	/**
	 * 单击菜单的向右按钮
	 * @param t 点击事件的对象
	 */
	onClickRight : function(t,n){
		if( (n-SinoPortal.NaviMenu.config.maxShowMenuNum) > SinoPortal.NaviMenu.config.leftMenuNum){
			SinoPortal.NaviMenu.config.leftMenuNum += 1;
			var left = SinoPortal.NaviMenu.config.leftMenuNum * 80 * -1;
			jQuery("#portalMainMenuBody").animate({left:left},500);
			
			// 修改左右的状态
			SinoPortal.NaviMenu.changeLeftRightClass(n);
		}
	},
	
	onMouseOverRight : function(t,n){
		if( (n-SinoPortal.NaviMenu.config.maxShowMenuNum) > SinoPortal.NaviMenu.config.leftMenuNum){
			jQuery(t).addClass('ui-state-hover');
		}
	},
	
	/**
	 * 单击菜单的向右按钮
	 * @param t 点击事件的对象
	 */
	onClickLevel1 : function(t){
		jQuery(".portal-menu-level1-main-btn").removeClass("ui-state-active");
		jQuery(t).addClass('ui-state-active');
		jQuery(".portal-objectView").hide();
		jQuery("[id='objectView:"+t.id+"_ap']").fadeIn(700);
	},
	
	/**
	 * 菜单点击执行ajax提交后调用的事件
	 * @param t 点击事件的对象
	 */
	onClickMenuItemComplete : function(xhr, status, args){
		if(args.validationFailed){
			SinoPortal.Confirm.showByMessages(args.msgs);
		}else{
			if(args.actionType=='tab'){
				main_tabSet_var.add(undefined, args);
				// TODO
			} else if(args.actionType=='window') {
				// 创建Win
				var winId = args.winId;
				var winContentId = args.winContentId;
				var onShow='',onHide='';
				if(args.onShow)onShow=args.onShow;
				if(args.onHide)onHide=args.onHide;
				var cfg = {autoOpen:true,dialogClass:'portal-popup-window'};
				cfg.onShow=function(){SinoPortal.View.winOnShow(winId,winContentId,onShow);};
				cfg.onHide=function(){SinoPortal.View.winOnHide(winId,winContentId,onHide);};
				if(args.width)cfg.width=args.width;
				if(args.height)cfg.height=args.height;
				if(args.minWidth)cfg.minWidth=args.minWidth;
				if(args.minHeight)cfg.minHeight=args.minHeight;
				cfg.showHeader=args.showHeader;//
				cfg.closable=args.closable;
				cfg.maximizable=args.maximizable;//
				cfg.minimizable=args.minimizable;//
				cfg.draggable=args.draggable;
				cfg.modal=args.modal;
				cfg.resizable=args.resizeable;
				var winsDiv = $("#portal_window"),
				win = $("<div id='"+args.winId+"' style='display:none;' class='ui-dialog ui-widget ui-widget-content ui-corner-all ui-helper-hidden portal-popup-window'></div>");
				if(args.showHeader){
					var winTitle = $("<div class='ui-dialog-titlebar ui-widget-header ui-helper-clearfix'></div>");
					winTitle.append($("<span class='ui-dialog-title'>"+args.displayName+"</span>"));
					if(args.closable)winTitle.append($("<a href='#' class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all'><span class='ui-icon ui-icon-closethick'></span></a>"));
					if(args.maximizable)winTitle.append($("<a href='#' class='ui-dialog-titlebar-icon ui-dialog-titlebar-maximize ui-corner-all'><span class='ui-icon ui-icon-extlink'></span></a>"));
					if(args.minimizable)winTitle.append($("<a href='#' class='ui-dialog-titlebar-icon ui-dialog-titlebar-minimize ui-corner-all'><span class='ui-icon ui-icon-minus'></span></a>"));
					win.append(winTitle);
				}
				win.append($("<div class='ui-dialog-content ui-widget-content'></div>"));
				// footer
				winsDiv.append(win);
				eval(winContentId+"_dialog=new PrimeFaces.widget.Dialog(args.winId,cfg);");
				
				
				// 动态加载
				var options = {
					source : winContentId,
			        update : winContentId
				};
				options.onsuccess = function(responseXML) {
					var xmlDoc = responseXML.documentElement,
			        updates = xmlDoc.getElementsByTagName("update");
					
			        for(var i=0; i < updates.length; i++) {
			            var id = updates[i].attributes.getNamedItem("id").nodeValue;
			            
			            if(id == winContentId){
			            	var content = updates[i].firstChild.data;
			            	jQuery("#"+winId).children(".ui-dialog-content").html(content);
			            }
			        }
			        return true;
				};
				PrimeFaces.ajax.AjaxRequest(options);
				
			}
		}
		statusDialog.hide();
	}
};

SinoPortal.Confirm = {
	showByMessages : function(msgs){
		var msgsObj = SinoPortal.Utils.evalJSON(msgs);
		var title='',content='';
		if(msgsObj.ERROR || msgsObj.FATAL){
			title = "错误";
			if(msgsObj.ERROR){
				for(var i=0;i<msgsObj.ERROR.length;i++){
					content =  content+msgsObj.ERROR[i]+'<br/>'
				}
			}
			if(msgsObj.FATAL){
				for(var i=0;i<msgsObj.FATAL.length;i++){
					content =  content+msgsObj.FATAL[i]+'<br/>'
				}
			}
			SinoPortal.Confirm.show("error",title,content);
		}else if(msgsObj.WARN){
			title = "警告";
			if(msgsObj.WARN){
				for(var i=0;i<msgsObj.WARN.length;i++){
					content =  content+msgsObj.WARN[i]+'<br/>'
				}
			}
			SinoPortal.Confirm.show("alert",title,content);
		}else if(msgsObj.INFO){
			title = "提示";
			if(msgsObj.WARN){
				for(var i=0;i<msgsObj.INFO.length;i++){
					content =  content+msgsObj.INFO[i]+'<br/>'
				}
			}
			SinoPortal.Confirm.show("info",title,content);
		}
	},
		
	/**
	 * 显示确认框
	 * @param type 类型
	 * 		alert 警告
	 * 		info 提示
	 * 		error 错误
	 * 		help 帮助
	 * @param title 标题
	 * @param content 内容
	 */
	show : function(type,title,content){
		if(type == 'alert'){
			if(!title)title="警告";
			confirmation.changeContent('alert',content);
		}else if(type == 'error'){
			if(!title)title="错误";
			confirmation.changeContent('circle-close',content);
		}else if(type == 'help'){
			if(!title)title="帮助";
			confirmation.changeContent('help',content);
		}else{
			if(!title)title="提示";
			confirmation.changeContent('info',content);
		}
		confirmation.changeTitle(title);
		confirmation.show();
	},
	
	showConfirmMethod : "",
	
	showConfirm : function(title,content,method){
		confirmation2.changeTitle(title);
		confirmation2.changeContent('info',content);
		SinoPortal.Confirm.showConfirmMethod = method;
		confirmation2.show();
	},
	
	showConfirmSubmit : function(){
		eval(SinoPortal.Confirm.showConfirmMethod);
		confirmation2.hide();
	}
};

SinoPortal.Validation = {
	validateForm : function(formId){ 
		var setting = {
		    showErrors: function showErrors(errors){ 
			if(errors) {
               
				for ( var i = 0; this.errorList[i]; i++ ) {
					var error = this.errorList[i];
					$.extend( this.errorMap, errors ); 
					for ( var name in errors ) { 
					    var ele = this.findByName(name)[0]; 
					    var elename = this.idOrName(error.element); 
					    var mg = error.message;
						jQuery(PrimeFaces.escapeClientId(elename)).addClass("sino-edit-error");
						jQuery(PrimeFaces.escapeClientId(elename)).parents("tr.sino-edit-tr").next().find("span.sino-edit-error").attr("style","display:block;");
						jQuery(PrimeFaces.escapeClientId(elename)).parents("tr.sino-edit-tr").next().find("div.sino-edit-div").empty(); 
						jQuery(PrimeFaces.escapeClientId(elename)).parents("tr.sino-edit-tr").next().find("div.sino-edit-div").append(mg); 
					}
					 
				}	
				 
				// 校验成功的去掉错误提示
				for ( var i = 0; this.successList[i]; i++ ) {
					
					jQuery(PrimeFaces.escapeClientId(this.idOrName(this.successList[i]))).removeClass("sino-edit-error");
					jQuery(PrimeFaces.escapeClientId(this.idOrName(this.successList[i]))).parents("tr.sino-edit-tr").next().find("span.sino-edit-error").attr("style","display:none;"); 
			    }
			} 
			 
		  }
		};
		
		jQuery(PrimeFaces.escapeClientId(formId)).validate(setting); 
		return jQuery(PrimeFaces.escapeClientId(formId)).valid();
		
	}, 


    validateFormForDocument : function(formId){ 
		var setting = {
		    showErrors: function showErrors(){
			var t = this;
			for ( var i = 0; this.errorList[i]; i++ ) {
				var error = this.errorList[i];
				this.settings.highlight && this.settings.highlight.call( this, error.element, "sino-validate-element-error", this.settings.validClass );
				var elename = this.idOrName(error.element);   
				// 错误信息div
				var errdiv = $('div[htmlfor="'+ elename + '"]');
				var errimg = $('img[htmlfor="'+ elename + '"]');
				if(errdiv.length == 0){ // 没有div则创建
					  
					// 带图片的圆角div在所有浏览器兼容
					errdiv = $('<div>' 
							+ '<img src="img/validate/left_icon.gif" width="6" height="24" align="absmiddle" style="float:left;" />'
							+ '<div class="validate-errmsgdiv errmsg" style="float:left;" ></div>'
							+ '<img src="img/validate/right_icon.gif" width="6" height="24" align="absmiddle" style="float:left;" />'
							+ '</div>');
					
					
					errdiv.attr({"for":  this.idOrName(error.element), generated: true})
					.addClass("sino-validate-div-error");
//					errdiv.css({left : $.getLeft(error.element) + 'px',top : $.getTop(error.element) + 'px'}); // 显示在控件的下面
					errdiv.appendTo($('body'));
				}
				if(errimg.length == 0){  
					 
					errimg.attr({"for":  this.idOrName(error.element), generated: true});
					errimg.insertAfter(error.element);
				}
				errimg.show();
				errdiv.find(".errmsg").html(error.message || ""); 
				// 鼠标放到控件上显示错误
				$(error.element).hover(function(e){
					$('div[htmlfor="'+ t.idOrName(this) + '"]').css({left : (e.pageX+20) + 'px',top : (e.pageY+20) + 'px'}); // 显示在鼠标位置偏移20的位置
					$('div[htmlfor="'+ t.idOrName(this) + '"]').fadeIn(200);
				},
				function(){
					$('div[htmlfor="'+ t.idOrName(this) + '"]').fadeOut(200);
				});
			}
			
			// 校验成功的去掉错误提示
			for ( var i = 0; this.successList[i]; i++ ) {
					$('div[htmlfor="'+ this.idOrName(this.successList[i]) + '"]').remove();
					$('img[htmlfor="'+ this.idOrName(this.successList[i]) + '"]').hide();
			}
			
			// 自定义高亮
			if (this.settings.unhighlight) {
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
					this.settings.unhighlight.call( this, elements[i], "sino-validate-element-error", this.settings.validClass );
				}
			}
		}
		};
		
		jQuery(PrimeFaces.escapeClientId(formId)).validate(setting);
		return jQuery(PrimeFaces.escapeClientId(formId)).valid();
	} 
};

SinoPortal.CodeSet = {
	onNodeClick : function(vbId,formId,elementId,node){
		var inputs = jQuery(node).find("span.ui-tree-node-label").find("input");
		var key = inputs[0].value;
		var name = inputs[1].value;
		if(key && key != ''){
			jQuery(PrimeFaces.escapeClientId(vbId+"_form:"+vbId+"_selectedKey")).val(key);
			jQuery(PrimeFaces.escapeClientId(vbId+"_form:"+vbId+"_selectedName")).val(name);
		}
	},
	
	rtnSelected : function(vbId,formId,elementId){
		var key = jQuery(PrimeFaces.escapeClientId(vbId+"_form:"+vbId+"_selectedKey")).val();
		var name = jQuery(PrimeFaces.escapeClientId(vbId+"_form:"+vbId+"_selectedName")).val();
		if(key && key != ''){
			jQuery(PrimeFaces.escapeClientId(formId+":"+elementId)).val(key);
			jQuery(PrimeFaces.escapeClientId(formId+":"+elementId+"_name")).val(name);
		}
		eval(vbId+"_dialog.hide();");
	},
	
	cleanSelected : function(vbId,formId,elementId){
		jQuery(PrimeFaces.escapeClientId(formId+":"+elementId)).val('');
		jQuery(PrimeFaces.escapeClientId(formId+":"+elementId+"_name")).val('');
		eval(vbId+"_dialog.hide();");
	}
};

SinoPortal.Func = {
	Setting : {
		System : {
			deleteOrg : function(compId){
				SinoPortal.Confirm.showConfirm('确认','是否删除该部门','#{compId}_deleteOrg("'+compId+'")')
			},
			deleteOrgUser : function(compId){
				var sel = SinoPortal.PageView.dataView.getSelected(compId+"_right_main");
				SinoPortal.Confirm.showConfirm('确认','是否删除将该用户从部门中删除','#{compId}_deleteOrgUser("'+compId+","+sel[0].id+'")');
			}
		}
	}
};